package b1.mobile.blackberry.screen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import b1.mobile.blackberry.dao.DataAccessDelegate;
import b1.mobile.blackberry.dao.StockDAO;
import b1.mobile.blackberry.entity.StockEntity;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.ui.component.InfoHeader;
import b1.mobile.blackberry.ui.component.ListGroupItemField;
import b1.mobile.blackberry.ui.component.ListGroupItemManager;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.FontFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;

public class StockInfoScreen extends BaseFunctionScreen implements DataAccessDelegate {
	private String id;	
	StockEntity ety = null;

	public StockInfoScreen(String id) {
		this.id = id;
		
		setTitle(FieldFactory.makeScreenTitle("Stock Info"));
		ThemeUtil.decorateScreen(this);
	}

	protected void onUiEngineAttached(boolean attached) {
		if (attached) {
			StockDAO dao = new StockDAO(this);
			dao.getItem(this.id);
		}
	}

	private void onLoadData() {
		ListGroupItemManager stockMgr = new ListGroupItemManager();
		stockMgr.setPadding(10, 0, 10, 0);
		
		Bitmap icon = Bitmap.getBitmapResource("_stock_detail.png");
		InfoHeader headerField = new InfoHeader(icon, ety.getItemName(), "", "");
		stockMgr.add(new ListGroupItemField(headerField));
		
		ListGroupItemField item = new ListGroupItemField(
				resources.getString(LABEL_ITEM_CODE), ety.getItemCode(),
				false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABLE_DESCRIPTION), ety.getStringProperty(StockEntity.JK_ITEMNAME), false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_UNIT_PRICE), ety.getStringProperty(StockEntity.JK_UNITPRICE), false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_IN_STOCK_QTY), ety.getStringProperty(StockEntity.JK_STOCKQTY), false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_SHIPPING_TYPE), ety.getStringProperty(StockEntity.JK_SHIPTYPE), false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_MANUFACTURER), ety.getStringProperty(StockEntity.JK_MANU), false);
		stockMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_SPEC), ety.getStringProperty(StockEntity.JK_USERTEXT), false);
		stockMgr.add(item);
		
		this.add(stockMgr);
	}

//	protected void onInStockQty() {
//		// TODO Auto-generated method stub
//		
//	}
//
//	protected void onUnitPrice() {
//		// TODO Auto-generated method stub
//		
//	}

	public void onDataReady(Serviceworker worker, Object data) {
		super.onDataReady(worker, data);
		if(worker.responseCode == 200){
			this.ety = (StockEntity)data;
			
			UiApplication.getUiApplication().invokeLater(new Runnable(){
				public void run(){
					onLoadData();
				}
			});
		}
	}
}
